
//
//  RBCollectionViewDefine.h
//  ZMH
//
//  Created by lanchengyu on 2019/8/20.
//  Copyright © 2019 lanchengyu. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "RBGroupModel.h"
#import "RBCellModel.h"
#import "RBBinder.h"

#pragma mark - 数据源&代理 item固定size

#define kRBCollectionViewDataSourceAndDelegate(dataSource) - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView\
{\
    return dataSource.count;\
}\
\
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section\
{\
    RBGroupModel *group = dataSource[section];\
    return group.cellModels.count;\
}\
\
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath\
{\
    RBGroupModel *group = dataSource[indexPath.section];\
    RBCellModel *cellModel = group.cellModels[indexPath.row];\
    cellModel.indexPath = indexPath;\
    if (cellModel.reuseCellIdentifier)\
    {\
        NSString *path = [[NSBundle mainBundle] pathForResource:cellModel.reuseCellIdentifier ofType:@"nib"];\
        if (path.length)\
        {\
            UINib *nib = [UINib nibWithNibName:cellModel.reuseCellIdentifier bundle:nil];\
            [collectionView registerNib:nib forCellWithReuseIdentifier:cellModel.reuseCellIdentifier];\
        }\
        else\
        {\
            [collectionView registerClass:NSClassFromString(cellModel.reuseCellIdentifier) forCellWithReuseIdentifier:cellModel.reuseCellIdentifier];\
        }\
    }\
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellModel.reuseCellIdentifier forIndexPath:indexPath];\
    [RBBinder bindCell:cell withCellModel:cellModel];\
    return cell;\
}\
\
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath\
{\
    RBGroupModel *group = dataSource[indexPath.section];\
    if ([kind isEqualToString:UICollectionElementKindSectionHeader])\
    {\
        if (group.headerReuseIdentifier.length)\
        {\
            NSString *path = [[NSBundle mainBundle] pathForResource:group.headerReuseIdentifier ofType:@"nib"];\
            if (path.length)\
            {\
                UINib *nib = [UINib nibWithNibName:group.headerReuseIdentifier bundle:nil];\
                [collectionView registerNib:nib forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:group.headerReuseIdentifier];\
            }\
            else\
            {\
                [collectionView registerClass:NSClassFromString(group.headerReuseIdentifier) forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:group.headerReuseIdentifier];\
            }\
            UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:group.headerReuseIdentifier forIndexPath:indexPath];\
            [RBBinder bindView:view withGroupHeaderModel:group];\
            return view;\
        }\
        else\
        {\
            return nil;\
        }\
    }\
    else if ([kind isEqualToString:UICollectionElementKindSectionFooter])\
    {\
        if (group.footerReuseIdentifier.length)\
        {\
            NSString *path = [[NSBundle mainBundle] pathForResource:group.footerReuseIdentifier ofType:@"nib"];\
            if (path.length)\
            {\
                UINib *nib = [UINib nibWithNibName:group.footerReuseIdentifier bundle:nil];\
                [collectionView registerNib:nib forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:group.footerReuseIdentifier];\
            }\
            else\
            {\
                [collectionView registerClass:NSClassFromString(group.footerReuseIdentifier) forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:group.footerReuseIdentifier];\
            }\
            UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:group.footerReuseIdentifier forIndexPath:indexPath];\
            [RBBinder bindView:view withGroupFooterModel:group];\
            return view;\
        }\
        else\
        {\
            return nil;\
        }\
    }\
    else\
    {\
        return nil;\
    }\
}\
\
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath\
{\
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];\
    RBGroupModel *group = dataSource[indexPath.section];\
    RBCellModel *cellModel = group.cellModels[indexPath.row];\
    if (cellModel.cellSelectBlock)\
    {\
        cellModel.cellSelectBlock(indexPath);\
    }\
}\
\
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath\
{\
    RBGroupModel *group = dataSource[indexPath.section];\
    RBCellModel *cellModel = group.cellModels[indexPath.row];\
    return cellModel.itemSize;\
}\
\
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section\
{\
    RBGroupModel *group = dataSource[section];\
    return group.sectionInset;\
}\
\
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section\
{\
    RBGroupModel *group = dataSource[section];\
    return group.minimumLineSpacing;\
}\
\
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section\
{\
    RBGroupModel *group = dataSource[section];\
    return group.minimumInteritemSpacing;\
}\
\
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section\
{\
    RBGroupModel *group = dataSource[section];\
    return group.headerReferenceSize;\
}\
\
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section\
{\
    RBGroupModel *group = dataSource[section];\
    return group.footerReferenceSize;\
}\

#pragma mark - 数据源&代理 item自动size

#define kRBCollectionViewDataSourceAndDelegateAuto(dataSource) - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView\
{\
    return dataSource.count;\
}\
\
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section\
{\
    RBGroupModel *group = dataSource[section];\
    return group.cellModels.count;\
}\
\
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath\
{\
    RBGroupModel *group = dataSource[indexPath.section];\
    RBCellModel *cellModel = group.cellModels[indexPath.row];\
    cellModel.indexPath = indexPath;\
    if (cellModel.reuseCellIdentifier)\
    {\
        NSString *path = [[NSBundle mainBundle] pathForResource:cellModel.reuseCellIdentifier ofType:@"nib"];\
        if (path.length)\
        {\
            UINib *nib = [UINib nibWithNibName:cellModel.reuseCellIdentifier bundle:nil];\
            [collectionView registerNib:nib forCellWithReuseIdentifier:cellModel.reuseCellIdentifier];\
        }\
        else\
        {\
            [collectionView registerClass:NSClassFromString(cellModel.reuseCellIdentifier) forCellWithReuseIdentifier:cellModel.reuseCellIdentifier];\
        }\
    }\
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellModel.reuseCellIdentifier forIndexPath:indexPath];\
    [RBBinder bindCell:cell withCellModel:cellModel];\
    return cell;\
}\
\
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath\
{\
    RBGroupModel *group = dataSource[indexPath.section];\
    if ([kind isEqualToString:UICollectionElementKindSectionHeader])\
    {\
        if (group.headerReuseIdentifier.length)\
        {\
            NSString *path = [[NSBundle mainBundle] pathForResource:group.headerReuseIdentifier ofType:@"nib"];\
            if (path.length)\
            {\
                UINib *nib = [UINib nibWithNibName:group.headerReuseIdentifier bundle:nil];\
                [collectionView registerNib:nib forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:group.headerReuseIdentifier];\
            }\
            else\
            {\
                [collectionView registerClass:NSClassFromString(group.headerReuseIdentifier) forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:group.headerReuseIdentifier];\
            }\
            UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:group.headerReuseIdentifier forIndexPath:indexPath];\
            [RBBinder bindView:view withGroupHeaderModel:group];\
            return view;\
        }\
        else\
        {\
            return nil;\
        }\
    }\
    else if ([kind isEqualToString:UICollectionElementKindSectionFooter])\
    {\
        if (group.footerReuseIdentifier.length)\
        {\
            NSString *path = [[NSBundle mainBundle] pathForResource:group.footerReuseIdentifier ofType:@"nib"];\
            if (path.length)\
            {\
                UINib *nib = [UINib nibWithNibName:group.footerReuseIdentifier bundle:nil];\
                [collectionView registerNib:nib forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:group.footerReuseIdentifier];\
            }\
            else\
            {\
                [collectionView registerClass:NSClassFromString(group.footerReuseIdentifier) forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:group.footerReuseIdentifier];\
            }\
            UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:group.footerReuseIdentifier forIndexPath:indexPath];\
            [RBBinder bindView:view withGroupFooterModel:group];\
            return view;\
        }\
        else\
        {\
            return nil;\
        }\
    }\
    else\
    {\
        return nil;\
    }\
}\
\
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath\
{\
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];\
    RBGroupModel *group = dataSource[indexPath.section];\
    RBCellModel *cellModel = group.cellModels[indexPath.row];\
    if (cellModel.cellSelectBlock)\
    {\
        cellModel.cellSelectBlock(indexPath);\
    }\
}\
\
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section\
{\
    RBGroupModel *group = dataSource[section];\
    return group.sectionInset;\
}\
\
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section\
{\
    RBGroupModel *group = dataSource[section];\
    return group.minimumLineSpacing;\
}\
\
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section\
{\
    RBGroupModel *group = dataSource[section];\
    return group.minimumInteritemSpacing;\
}\
\
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section\
{\
    RBGroupModel *group = dataSource[section];\
    return group.headerReferenceSize;\
}\
\
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section\
{\
    RBGroupModel *group = dataSource[section];\
    return group.footerReferenceSize;\
}\

